﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QLPKWeb.Models.LinQ.DanhMucChung;
using Webdiyer.WebControls.Mvc;
using DONGACS.Common;
using QLPKWeb.Models.FormBean.DanhMucChungModel;

namespace QLPKWeb.BussinessDelegate.DanhMucChung
{
    public class NCCDelegate
    {
        DBDanhMucChungDataContext DBDanhMucChung = new DBDanhMucChungDataContext();

        public IList<dm_NhaCC> GetAllNCC()
        {
            return DBDanhMucChung.dm_NhaCCs.ToList();
        }

        public dm_NhaCC AddNewNCC(dm_NhaCC NCC)
        {
            DBDanhMucChung.dm_NhaCCs.InsertOnSubmit(NCC);
            DBDanhMucChung.SubmitChanges();
            return GetNCCIdMax();
        }

        public void DeleteNCC(string NCCID)
        {
            dm_NhaCC NCCNew =
                DBDanhMucChung.dm_NhaCCs.Single(p => p.Id == Convert.ToInt32(NCCID));
            //NCC.Id = NCC.Id;

            DBDanhMucChung.dm_NhaCCs.DeleteOnSubmit(NCCNew);
            DBDanhMucChung.SubmitChanges();
        }

        public void UpdateNCC(dm_NhaCC NCC)
        {
            dm_NhaCC NCCUpdate =
                DBDanhMucChung.dm_NhaCCs.Single(p => p.Id == NCC.Id);
            NCCUpdate.Id = NCC.Id;
            NCCUpdate.TenNCC = NCC.TenNCC;
            NCCUpdate.DiaChi = NCC.DiaChi;
            NCCUpdate.SoDT = NCC.SoDT;
            NCCUpdate.Fax = NCC.Fax;
            NCCUpdate.Mail = NCC.Mail;
            NCCUpdate.Website = NCC.Website;
            NCCUpdate.MaST = NCC.MaST;
            NCCUpdate.NganHang = NCC.NganHang;
            NCCUpdate.TaiKhoan = NCC.TaiKhoan;
            NCCUpdate.IsKhuVuc = NCC.IsKhuVuc;
            NCCUpdate.IdMaNCC = NCC.IdMaNCC;
            NCCUpdate.LocalName = NCC.LocalName;
            NCCUpdate.ForeignName = NCC.ForeignName;
            DBDanhMucChung.SubmitChanges();
        }

        public PagedList<dm_NhaCC> SearchNCCPagedList(int? id, string searchCriteria)// id = page Index
        {
            //IQueryable<dm_NCC> list = DBDanhMucChung.dm_NCCs.Where(p => p.Ten == searchCriteria);
            //DBDanhMucChung.dm_NCCs.ToPagedList(list,)

            return (PagedList<dm_NhaCC>)DBDanhMucChung.dm_NhaCCs.ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage).Where(p => p.TenNCC == searchCriteria);

        }

        public dm_NhaCC GetNCCById(string id)
        {
            try
            {
                dm_NhaCC item = this.DBDanhMucChung.dm_NhaCCs.Single(p => p.Id == Convert.ToInt32(id));
                return item;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public dm_NhaCC GetNCCByTen(string ten)
        {
            //dm_NCC item = this.DBDanhMucChung.dm_NCCs.Where(p => p.Ten == ten).Count() != 0 ? this.DBDanhMucChung.dm_NCCs.Where(p => p.Ten == ten).ElementAt(0) : null ;

            IEnumerable<dm_NhaCC> list = this.DBDanhMucChung.dm_NhaCCs.Where(p => p.TenNCC == ten);
            return list.Count() != 0 ? list.ElementAt(0) : null;
        }

        public PagedList<dm_NhaCC> GetNCCPagedList(int? id)// id = page Index
        {
            return DBDanhMucChung.dm_NhaCCs.ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);
        }

        public dm_NhaCC BuildNewNCC(NCCForm form)
        {
            dm_NhaCC newNCC = new dm_NhaCC();
            newNCC.TenNCC = form.TenNCC;
            newNCC.DiaChi = form.DiaChi;
            newNCC.SoDT = form.SoDT;
            newNCC.Fax = form.Fax;
            newNCC.Mail = form.Mail;
            newNCC.Website = form.Website;
            newNCC.MaST = form.MaST;
            newNCC.NganHang = form.NganHang;
            newNCC.TaiKhoan = form.TaiKhoan;
            newNCC.IsKhuVuc = form.IsKhuVuc;
            newNCC.IdMaNCC = form.IdMaNCC;
            newNCC.LocalName = form.LocalName;
            newNCC.ForeignName = form.ForeignName;
            return newNCC;
        }

        public dm_NhaCC BuildEditNCC(NCCForm form)
        {
            dm_NhaCC newNCC = new dm_NhaCC();
            newNCC.Id = form.Id;
            newNCC.TenNCC = form.TenNCC;
            newNCC.DiaChi = form.DiaChi;
            newNCC.SoDT = form.SoDT;
            newNCC.Fax = form.Fax;
            newNCC.Mail = form.Mail;
            newNCC.Website = form.Website;
            newNCC.MaST = form.MaST;
            newNCC.NganHang = form.NganHang;
            newNCC.TaiKhoan = form.TaiKhoan;
            newNCC.IsKhuVuc = form.IsKhuVuc;
            newNCC.IdMaNCC = form.IdMaNCC;
            newNCC.LocalName = form.LocalName;
            newNCC.ForeignName = form.ForeignName;

            return newNCC;
        }

        public dm_NhaCC GetNCCIdMax()
        {
            IList<dm_NhaCC> list = DBDanhMucChung.dm_NhaCCs.ToList();
            return list.Count != 0 ? list.ElementAt(list.Count - 1) : null;
        }
        public SelectList BuilNCC(int? selectedValue)
        {
            IList<dm_NhaCC> selectNCC = DBDanhMucChung.dm_NhaCCs.OrderBy(p => p.Id).ToList();
            IList<object> selectList = new List<object>();
            int dem = 0;
            foreach (var o in selectNCC)
            {
                dem++;
                if (selectedValue != 0 || dem == selectedValue)
                {
                    selectList.Add(new { value = o.Id, Text = o.TenNCC, SelectedValue = selectedValue });
                }
                else
                {
                    selectList.Add(new { value = o.Id, Text = o.TenNCC });
                }
            }
            SelectList select = new SelectList(selectList, "Value", "Text", selectedValue);
            return select;
        }
    }
}